<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../static/lib/layui-v2.9.8/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>查询条件</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">用户ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="uid" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">请求方法</label>
                            <div class="layui-input-inline">
                                <input type="text" name="method" autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">URL</label>
                            <div class="layui-input-inline">
                                <input type="text" name="url" autocomplete="off" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">描述</label>
                            <div class="layui-input-inline">
                                <input type="text" name="desc" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">登陆时间</label>
                            <div class="layui-input-inline">
                                <input type="text" id="startTime" name="create_time" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <select name="success">
                                    <option value=""></option>
                                    <option value="1">成功</option>
                                    <option value="0">失败</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-normal" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 查 询
                            </button>

                            <button type="reset" class="layui-btn layui-btn-primary"><i
                                    class="layui-icon layui-icon-refresh"></i> 重 置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarUser">
            <div class="layui-btn-container">
                {% if "login-log:delete" in request.session.permissions %}
                    <button class="layui-btn layui-btn-sm layui-btn-primary layui-border-red data-delete-btn"
                            lay-event="multi-del"> 批量删除
                    </button>
                {% endif %}
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>


        <script type="text/html" id="logStatus">
            {% verbatim %}
            {{#  if(d.success == true){ }}
            <span style="color: green">成功</span>
            {{#  } else { }}
            <span style="color: red">失败</span>
            {{#  } }}
            {% endverbatim %}
        </script>

    </div>
</div>
<script src="../../../static/lib/layui-v2.9.8/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;

        laydate.render({
            elem: '#startTime',
            type: 'datetime',
            format: 'yyyy-MM-dd HH:mm:ss',
        });

        var token = "{{ csrf_token }}";

        $.ajaxSetup({
            headers: {
                'X-CSRFTOKEN': `${token}`,
                'AUTHORIZATION': 'Bearer ' + localStorage.token,
            }, // JWTAuthentication.authenticate()方法获取 header = request.META.get(api_settings.AUTH_HEADER_NAME),
        });

        $.ajaxSetup({
            complete: function (XMLHttpRequest, textStatus) {
                if (XMLHttpRequest.status === 401) {  // 401 表示未授权，即 token 无效
                    layer.msg('授权已过期，需重新登陆', {icon: 2, time: 2000}, function () {
                        window.location.href = '/login';
                    })
                }
            }
        });


        table.render({
            elem: '#currentTableId',
            url: '/login_log/',
            method: 'get',
            toolbar: '#toolbarUser',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'count', width: 100, title: '序号', align: 'center', sort: true},
                {field: 'id', width: 100, title: 'ID', align: 'center', sort: true, hide: true},
                {field: 'uid', width: 100, title: '用户ID', align: 'center', sort: true, hide: false},
                {field: 'method', width: 100, title: '请求方法', align: 'center', sort: true},
                {field: 'url', width: 120, title: '请求URL', align: 'center', sort: true},
                {field: 'ip', width: 180, title: 'IP', align: 'center', sort: true},
                {field: 'create_time', width: 200, title: '登陆时间', align: 'center'},
                {field: 'user_agent', title: '用户代理', width: 480, minWidth: 150, align: 'center', sort: true},
                {field: 'desc', width: 220, title: '操作信息', align: 'center', sort: true},
                {field: 'success', width: 100, title: '是否成功', align: 'center', sort: true, templet: '#logStatus'},
                // {title: '操作', minWidth: 0, toolbar: '#currentTableBar1', align: "center"}

            ]],
            limits: [10, 20, 50, 100, 200, 500],
            limit: 10,
            page: true,
            skin: 'row',
            even: true,
            parseData: function (res) { //res 即为原始返回的数据
                console.log(res);
            },
            before: function (res, curr, count) {
            },
            done: function (res, curr, count) {

            }

        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            // console.log(data.field);
            var result = JSON.stringify(data.field);

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    Params: result
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'multi-del') {  // 监听multi-del操作
                var checkStatus = table.checkStatus('currentTableId');
                var data = checkStatus.data;
                if (data.length === 0) {
                    layer.msg('未选中数据', {
                        icon: 3,
                        time: 1000
                    })
                    return false
                } else {
                    layer.confirm('真的删除选中行吗?', {icon: 3, title: '确认删除'}, function (index) {
                        layer.close(index);

                        if (data.length === 0) {
                            layer.msg('请选择需要删除的数据!', {
                                icon: 3,
                                time: 1000
                            })
                            return false
                        }
                        //传给后台
                        var URL = '/login_log/multiple_delete/'
                        $.ajax({
                            url: URL,
                            type: "delete",
                            data: {Params: JSON.stringify(data)},
                            success: function (result) {
                                if (result.success) {
                                    // layer.msg(result.msg);
                                    layer.msg(result.msg, {icon: 1, time: 2000}, function () {
                                        parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                                        table.reload('currentTableId');  //表格重载
                                    })
                                } else {
                                    layer.msg(result.msg);
                                }

                            },
                            error: function (result) {
                                alert("接口错误！！");
                            }
                        });
                        return false;
                    });

                }
            } else if (obj.event === 'refresh') {
                window.refresh()
            }

        });


    });
</script>

</body>
</html>